package com.jwdf.uda.view.prompt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemImageTxtQuestionBinding;
import com.jwdf.uda.databinding.ItemOnlyQuestionBinding;
import com.jwdf.uda.databinding.ItemTxtQuestionBinding;
import com.jwdf.uda.databinding.ViewPromptNewHomeBinding;
import com.jwdf.uda.databinding.ViewQuestionBottomBinding;
import com.jwdf.uda.databinding.ViewQuestionTopBinding;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.utils.AppBus;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.PromptUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.dialog.BaseDialog;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2017/1/13.
 */
public class NewHomePrompt extends BaseDialog implements DialogInterface.OnDismissListener {
    private ViewPromptNewHomeBinding mBinding;
    private View mHomeListView;
    private View mHomeHeadView;
    private AbsViewHolder mAbsViewHolder;
    private int deviceWidth;
    private boolean mCancelable;
    private PbBean.PbBaseQuestion mBaseQuestion;

    public NewHomePrompt(Context context, View homeListView, View homeHeadView , PbBean.PbBaseQuestion baseQuestion) {
        super(context);
        this.mHomeListView = homeListView;
        this.mHomeHeadView = homeHeadView;
        this.mBaseQuestion=baseQuestion;
        this.deviceWidth = DisplayUtils.getDeviceWidth(context);
    }

    public NewHomePrompt(Context context, View homeListView, View homeHeadView, PbBean.PbBaseQuestion baseQuestion , boolean cancelable) {
        this(context, homeListView, homeHeadView,baseQuestion);
        mCancelable = cancelable;
    }

    private Handler handler = new Handler();

    private Runnable mListRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mHomeListView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mHomeListView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.ivListTxt);
//            location[1] -=viewHeight;
//            viewHeight += DisplayUtils.dip2px(getContext(), 8);
            mAbsViewHolder=onCreateView();
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(deviceWidth, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.topMargin = location[1];
            params.leftMargin = 0;
            params.rightMargin = 0;
            params.bottomMargin = 0;
            mBinding.rl.addView(mAbsViewHolder.getConvertView(),params);
//            params.topMargin = location[1];
//            params.leftMargin = 0;
//            params.rightMargin = 0;
//            params.bottomMargin = 0;
//            mBinding.ivList.setPadding(0,location[1],0,0);
//            View a=mAbsViewHolder.getConvertView();
//            a.setLayoutParams(params);
//            mBinding.ivList.setLayoutParams(params);
//            mBinding.ivList.addView(a,params);
            // mBinding.ivList.setPadding(0,location[1],0,0);
            mBinding.ivList.setVisibility(View.VISIBLE);
            mBinding.ivListTxt.setVisibility(View.VISIBLE);
            int viewListHeight = PromptUtil.getMeasuredHeight(mBinding.ivList);
            location[1] += viewHeight;

//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, mBinding.ivList.getId());
            p.leftMargin=DisplayUtils.dip2px(getContext(), 35);
            p.topMargin=location[1]+ DisplayUtils.dip2px(getContext(), 0);
//            mBinding.ivListTxt.setPadding(DisplayUtils.dip2px(getContext(), 35), 0, 0, DisplayUtils.dip2px(getContext(), 0));
            mBinding.ivListTxt.setLayoutParams(p);
        }
    };


    private Runnable mHeadRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mHomeHeadView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mHomeHeadView);
            int viewTextHeight = PromptUtil.getMeasuredHeight(mBinding.llHead);
            viewHeight -= DisplayUtils.dip2px(getContext(), 8);
//            location[1] -=viewHeight;
            location[1] -= viewTextHeight;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(deviceWidth, viewHeight);
            params.addRule(RelativeLayout.BELOW, mBinding.llHead.getId());
            mBinding.ivHead.setLayoutParams(params);

            mBinding.ivHead.setVisibility(View.VISIBLE);
            mBinding.llHead.setVisibility(View.VISIBLE);
//            mBinding.ivGuide.setPadding(0, 0, 0, 0);
            mBinding.llHead.setPadding(DisplayUtils.dip2px(getContext(), 0), location[1] - DisplayUtils.dip2px(getContext(), 0), 0, DisplayUtils.dip2px(getContext(), 0));
        }
    };

    private Runnable mBtnRunnable = new Runnable() {
        @Override
        public void run() {
//            mBinding.ivBtn.setVisibility(View.VISIBLE);
//            mBinding.ivBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dismiss();
//                }
//            });
            mBinding.rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    };

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_prompt_new_home, null, false);
        mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (View.VISIBLE == mBinding.ivBtn.getVisibility()) {
                    dismiss();
                }
            }
        });

        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        setOnDismissListener(this);
        handler.postDelayed(mHeadRunnable, 500);
        handler.postDelayed(mListRunnable, 1000);
        handler.postDelayed(mBtnRunnable, 1000);
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(mListRunnable);
        handler.removeCallbacks(mBtnRunnable);
        handler.removeCallbacks(mHeadRunnable);
        PromptUtil.setHideHomePrompt(getContext());
        AppBus.getInstance().post(getContext());
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }

    private AbsViewHolder onCreateView(){
        if(!mBaseQuestion.hasBestAnswer()){
            QuestionHolder questionHolder=   new QuestionHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_only_question,mBinding.rl,false));
            questionHolder.onBindView(mBaseQuestion);
            return questionHolder;
        }else if(mBaseQuestion.getBestAnswer().getQuickImageInfoList() == null || mBaseQuestion.getBestAnswer().getQuickImageInfoList().isEmpty()){
            TextHolder textHolder= new TextHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_txt_question, mBinding.rl, false));
            textHolder.onBindView(mBaseQuestion);
            return textHolder;
        }else {
            ImageTextHolder imageTextHolder= new ImageTextHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_image_txt_question, mBinding.rl, false));
            imageTextHolder.onBindView(mBaseQuestion);
            return imageTextHolder;
        }
    }


    private class QuestionHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemOnlyQuestionBinding mBinding;

        public QuestionHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);

        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            mBinding.promptDiv.setVisibility(View.GONE);
            onBindBaseTopItem(convertView, mBinding.topLayout, item);
        }
    }

    private class TextHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemTxtQuestionBinding mBinding;

        public TextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            mBinding.promptDiv.setVisibility(View.GONE);
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, item);
            mBinding.tvInfo.setText(StringUtils.filterNullAndTrim(item.getBestAnswer().getQuickDescription()));
        }
    }

    private class ImageTextHolder extends AbsViewHolder<PbBean.PbBaseQuestion> {
        private ItemImageTxtQuestionBinding mBinding;

        public ImageTextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(final PbBean.PbBaseQuestion item) {
            mBinding.promptDiv.setVisibility(View.GONE);
            onBindCommonView(convertView, mBinding.topLayout, mBinding.bottomLayout, item);
            mBinding.tvInfo.setText(StringUtils.filterNullAndTrim(item.getBestAnswer().getQuickDescription()));
            mBinding.tvThumbNum.setText(item.getBestAnswer().getQuickImageCount() + "张");
            ImageLoader.getInstance().displayImage(item.getBestAnswer().getQuickImageInfoList().get(0).getUrl(), mBinding.ivThumb);
        }
    }

    private void onBindCommonView(View convertView, ViewQuestionTopBinding topLayout, ViewQuestionBottomBinding bottomLayout, final PbBean.PbBaseQuestion item) {
        onBindBaseTopItem(convertView, topLayout, item);
        final PbBean.PbUserSimple userInfo = item.getBestAnswer().getUserInfo();
        ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), bottomLayout.ivHeader);
        bottomLayout.ivDesigner.setVisibility(userInfo.getIsDesigner() ? View.VISIBLE : View.GONE);
        bottomLayout.tvName.setText(StringUtils.filterNullAndTrim(userInfo.getNickname()));
        bottomLayout.tvAdmireNum.setText(String.valueOf(item.getBestAnswer().getLikeCount()));
        bottomLayout.tvAdmireNum.setVisibility(item.getBestAnswer().getLikeCount() > 0 ? View.VISIBLE : View.GONE);
        bottomLayout.ivReplyLevel.setVisibility(item.getBestAnswer().getLikeCount() > 6 ? View.VISIBLE : View.GONE);

    }

    @SuppressLint("StringFormatMatches")
    private void onBindBaseTopItem(final View convertView, ViewQuestionTopBinding topLayout, final PbBean.PbBaseQuestion item) {
        if (item.hasActionStr() && item.hasActionUserInfo()) {
            PbBean.PbUserSimple userInfo = item.getActionUserInfo();
            ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), topLayout.ivHeader);
            topLayout.ivDesigner.setVisibility(userInfo.getIsDesigner() ? View.VISIBLE : View.GONE);
            topLayout.tvOperateTxt.setText(item.getActionStr());
            topLayout.tvName.setText(userInfo.getNickname());
            topLayout.tvIntro.setText(userInfo.getIntroduction());
            topLayout.tvIntro.setVisibility(!TextUtils.isEmpty(StringUtils.filterNullAndTrim(userInfo.getIntroduction())) ? View.VISIBLE : View.GONE);
            topLayout.llActionLayout.setVisibility(View.VISIBLE);
        } else {
            topLayout.llActionLayout.setVisibility(View.GONE);
        }
        topLayout.tvCount.setText(convertView.getContext().getString(R.string.item_top_count, item.getPv(), item.getConcernCount(), item.getAnswerCount()));
        topLayout.tvTitle.setText(StringUtils.filterNullAndTrim(item.getTitle()));
    }
}
